//
//  GifloginViewController.m
//  SpeedDating
//
//  Created by mac on 2017/5/9.
//  Copyright © 2017年 isu. All rights reserved.
//

#import "GifloginViewController.h"
#import "UIImage+GIF.h"
#import "LoginViewController.h"
#import "MZCTabBarController.h"

#import <TencentOpenAPI/QQApiInterface.h>
#import "WXApi.h"
#import "WeiboSDK.h"
#import <UMSocialCore/UMSocialCore.h>

@interface GifloginViewController ()
@property (nonatomic, strong)UIView *BgView; //背景
@property (nonatomic, strong)UIImageView *gifImage;//动图
@property (nonatomic, strong)UIView *leftV, *rightV;//左右线
@property (nonatomic, strong)UILabel *CenterLab;//提示
@property (nonatomic, strong)UIButton * UMbtn;




@end

@implementation GifloginViewController
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = YES;
    self.BgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    self.BgView.backgroundColor = Colorbackground;
    [self.view addSubview:_BgView];
    
    _gifImage = [UIImageView new];
    
//    NSString  *name = @"searchGif_YD.gif";
//    
//    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
//    
//    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    
//    self.gifImage.image =  [UIImage sd_animatedGIFWithData:imageData];
    
    
    self.gifImage.image = [UIImage imageNamed:@"logo"];
    [_BgView addSubview:_gifImage];
    
    
    [_gifImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_BgView).offset(0);
        make.top.mas_equalTo(_BgView).offset(123*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(132.5*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(120.5*SCREEN_WIDTH_RATIO47);

    }];
    
    
    CGFloat btnW = 55*SCREEN_WIDTH_RATIO47;
    CGFloat btnH = 55*SCREEN_WIDTH_RATIO47;
    
    CGFloat btnK = (KScreenWidth-btnW*2)/3;
    
    
    _CenterLab = [[UILabel alloc]initWithFrame:CGRectMake(0, (243.5+214)*SCREEN_WIDTH_RATIO47, KScreenWidth, 12*SCREEN_WIDTH_RATIO47)];
    _CenterLab.textAlignment = NSTextAlignmentCenter;
    
    _CenterLab.text = @"选择登录方式";
    
    _CenterLab.textColor = ColorI(0xcc2e353f);
    _CenterLab.font=FontP(15);
    [_BgView addSubview:_CenterLab];
    
    
    


    for (int i = 0; i <2; i++) {
        self.UMbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (i==0 && ![WXApi isWXAppInstalled]) {
            continue;
        }
        if (i==1 && ![QQApiInterface isQQInstalled]) {
            continue;
        }
//        if (i==2 && ![WeiboSDK isWeiboAppInstalled]) {
//            continue;
//        }

        self.UMbtn.frame = CGRectMake(btnK+(i*(btnW+btnK)), _CenterLab.bottom+13.5*SCREEN_WIDTH_RATIO47, btnW, btnH);
        self.UMbtn.tag = i;
        [self.UMbtn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            [self.UMbtn setImage:[UIImage imageNamed:@"weixin"] forState:UIControlStateNormal];

        }else if (i == 1){
            [self.UMbtn setImage:[UIImage imageNamed:@"QQ"] forState:UIControlStateNormal];

        }
//        else{
//            [self.UMbtn setImage:[UIImage imageNamed:@"weibo"] forState:UIControlStateNormal];
//        }
        [_BgView addSubview:self.UMbtn];
    }

    
    UIButton  *phoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    phoneBtn.titleLabel.font = FontP(15);

    phoneBtn.frame = CGRectMake(KScreenWidth/4,self.view.height-98*SCREEN_WIDTH_RATIO47, KScreenWidth/2, 20*SCREEN_WIDTH_RATIO47);
    [phoneBtn addTarget:self action:@selector(LoginViewClick) forControlEvents:UIControlEventTouchUpInside];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"手机登录或注册"];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    [phoneBtn setAttributedTitle:str forState:UIControlStateNormal];
    phoneBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [phoneBtn setTitleColor:ColorI(0xcc2e353f) forState:UIControlStateNormal];

    [_BgView addSubview:phoneBtn];

    UILabel *ComLab = [[UILabel alloc] initWithFrame:CGRectMake(0, phoneBtn.bottom+38*SCREEN_WIDTH_RATIO47, KScreenWidth, 30*SCREEN_WIDTH_RATIO47)];
    ComLab.font = FontP(10);
    ComLab.textColor = ColorI(0xdfa300);
    ComLab.text = @"conpright@2017 tielianer.com";
    ComLab.textAlignment = NSTextAlignmentCenter;
    [_BgView addSubview:ComLab];
}


#pragma mark - 手机登录或注册
- (void)LoginViewClick{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:YES];
    
    
}
#pragma mark - UMShare（三方登录接口）

- (void)btnclick:(UIButton *)sender{
    if (sender.tag == 0){
        [self getUserInfoForPlatform:UMSocialPlatformType_WechatSession];
        
    }else if (sender.tag == 1){
        [self getUserInfoForPlatform:UMSocialPlatformType_QQ];
        
    }else{
        [self getUserInfoForPlatform:UMSocialPlatformType_Sina];
        
    }
}

- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
    
    [[UMSocialManager defaultManager] cancelAuthWithPlatform:platformType completion:^(id result, NSError *error) {
        [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error) {
            
            UMSocialUserInfoResponse *resp = result;
            
            // 授权信息
            NSLog(@"Wechat uid: %@", resp.uid);
            NSLog(@"Wechat openid: %@", resp.openid);
            NSLog(@"Wechat accessToken: %@", resp.accessToken);
            NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
            NSLog(@"Wechat expiration: %@", resp.expiration);
            
            // 用户信息
            NSLog(@"Wechat name: %@", resp.name);
            NSLog(@"Wechat iconurl: %@", resp.iconurl);
            NSLog(@"Wechat gender: %@", resp.unionGender);
            
            // 第三方平台SDK源数据
            NSLog(@"Wechat originalResponse: %@", resp.originalResponse);
            if(resp.uid.length){
                //调用：登陆接口
                [self loginWithType:platformType withUid:resp.uid nickname:resp.name];
            }else{
                [SVProgressHUD showErrorWithStatus:@"登陆失败!"];
            }
    
        }];
    }];
   
}




#pragma mark - loginWithType
-(void)loginWithType:(UMSocialPlatformType)platformType withUid:(NSString *)uid nickname:(NSString *)nickname{
    NSString *str = nil;
    switch (platformType) {
        case UMSocialPlatformType_WechatSession:
            str = @"wechat";
            break;
        case UMSocialPlatformType_QQ:
            str = @"qq";
            break;
//        case UMSocialPlatformType_Sina:
//            str = @"weibo";
//            break;
        default:
            break;
    }
    [SVProgressHUD show];
    __weak __typeof(self) blockSelf = self;
    NSString* identifierNumber = [[UIDevice currentDevice].identifierForVendor UUIDString] ;
    NSLog(@"手机序列号: %@",identifierNumber);
    [HQHttpManager userLoginWithAccout:nil password:nil thirdNO:uid thirdType:str mode:@"third" nickName:nickname deviceNo:identifierNumber successBlock:^(BOOL isSuccess, NSDictionary *resultDic) {
        [SVProgressHUD dismiss];
        NSLog(@"resulDic登录回调======%@",resultDic);
        if (isSuccess) {
            [SVProgressHUD showWithStatus:@"登录成功"];
      
                [blockSelf loginSuccess];

        } else {
            [SVProgressHUD showWithStatus:@"登录失败"];
        }
    }];
}


-(void)loginSuccess{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_isLogin object:@(NO)];

    [SVProgressHUD dismiss];
}

@end
